<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Articles Manager
 * @author	Tran Van Thanh
 * @mail	thanhtran@vietnambiz.com
 * @date	06.09.2011
 */
require_once('admin'.EXT);

class Table_price extends Admin {

	function __construct()
	{
		parent::__construct();
		$this->load->model(array('tableprice_model'));
		$this->load->helper(array('upload','text'));			
	}

	function index()
	{
		$page = (isset($_GET['p']) && is_numeric($_GET['p'])) ? $_GET['p'] : 1;

        if(isset($_GET['status'])) {
            $status = ($_GET['status'] == 'active') ? 'yes' : 'no';
            $this->db->where('status', $status);
        }		
		

		$this->data['articles'] 	= $this->tableprice_model->get_articles($page != "" ? $page : 1);
		$this->data['page'] 		= $page;

    	$conf =	array(
    		'base_url'		=> admin_url($this->router->class).'/',
    		'total_rows'	=> $this->tableprice_model->count_all(),
    		'per_page'		=> $this->config->item('articles', 'limit'),
    		'cur_page'		=> $page,
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav'] = $this->pagination->display_query_string();

		if($this->input->is_ajax_request()) {
			$this->load->view('tableprice/list', $this->data);
		}
		else {
			$this->data['tpl_file']	= 'tableprice/index';
			$this->load->view('layout/default', $this->data);
		}
	}

	function add()
	{		
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
			$this->form_validation->set_message('required', '%s không hợp lệ!');
    		$this->form_validation->set_rules('txt_title', 'Tiêu đề', 'required|trim|xss_clean');

    		if($this->form_validation->run() == TRUE)
            {
            	if(isset($_FILES["uploadedfile"]["name"])) 
            	{
            		$title 	= $this->input->post('txt_title');
            		$target_path = "uploads/files/";
            		if(!is_dir($target_path))
            		{
            			create_dir($target_path);
            		}
					$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
            		$ext = get_ext($_FILES["uploadedfile"]["name"]);
            		
	                    if(!in_array($ext, array('PDF', 'xls', 'xlsx', 'doc','rar','zip','pdf'))) {
	               			continue;
	                    }
	                   
					if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
					{
					  $data = array(
	            			'name'			=> strip_tags($title),
		            		'name_link'		=> ascii_link($title,'-'),		            	 
            				'file_path'		=> $target_path,    
					  		'date_create'	=> date('Y-m-d H:i:s'),       		    		          			        		
		            		'status'		=> $this->input->post('status')
	            		);	   
	            		    			            	
	            		$articles_id = $this->tableprice_model->insert($data);
	            		die('yes');	 
            	    }	            
            	    else
            	    {die('Co loi xay ra!');}		            	        
            }else {die('Bạn hãy chọn file để uploads !');}	
           }else {die(validation_errors());}	
		}
		$this->load->view('tableprice/add', $this->data);
	}

	function edit($articles_id = null)
	{		
		$articles = $this->tableprice_model->get($articles_id);
				
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
    		$this->form_validation->set_rules('txt_title', 'Tiêu đề', 'required|trim|xss_clean');

    		if($this->form_validation->run() == TRUE)
            {       
            	$title 	= $this->input->post('txt_title');
            	$target_path = "uploads/files/";
            	if(!is_dir($target_path))
            	{
            		create_dir($target_path);
            	}
            	$file          = $_FILES["uploadedfile"]["name"];
                $old_file      = $this->input->post('file_old');
              	$data_articles = array(
            		'name'				=> strip_tags($title),            	            		    		          			        		
            		'status'			=> $this->input->post('status')
            	);            	
            
            	$this->tableprice_model->update($articles_id,$data_articles);  
            
               	if($file != $old_file && $file != '' && $file != $target_path)
                {                 	
                	if(isset($_FILES["uploadedfile"]["name"])) 
            		{	 
						$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
					
	            	    $ext = get_ext($_FILES["uploadedfile"]["name"]);
            			if(!in_array($ext, array('PDF', 'xls', 'xlsx', 'doc','rar','zip','pdf'))) {
	               			continue;
            			}
	                    	
	                    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
	            	    	$data = array('file_path' 		=> $target_path);
	            	    	
	            			$this->db->where('id', $articles_id)->update('table_price', $data);
	            			
	            			if(is_file(dirname($_SERVER['SCRIPT_FILENAME']).'/'.$articles->file_path)){  
	            				unlink($articles->file_path); 					                
	            			}            		
	                    }  
            		}	
                }       
                	die('yes');     	            	            	
            }
            else {
            	die(validation_errors());
            }
		}

		$this->data['articles'] = $articles;
		$this->load->view('tableprice/edit', $this->data);
	}
	
	function do_action()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') {

			$id_list = $this->input->post('id');
			$action = $this->input->post('action');

			if($action == 'delete') {
				$this->delete($id_list);
			}
			elseif($action == 'pending') {
				$this->tableprice_model->update_many($id_list, array('status' => 'no'));
			}
			elseif($action == 'active') {
				$this->tableprice_model->update_many($id_list, array('status' => 'yes'));
			}
			die('yes');
		}
	}

	function change_status()
	{
		$articles_id = $this->input->post('price_id');
		$status 	 = $this->input->post('status');

		$this->tableprice_model->update($articles_id, array('status' => $status));
		die('yes');
	}	

	function load_row($articles_id = null)
	{		
		$this->data['articles'] = $this->tableprice_model->get($articles_id);
		$this->load->view('tableprice/row', $this->data);
	}	
	

    function delete($articles_id = null)
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST') 
        {
        	$articles = $this->tableprice_model->get($articles_id);        	      
			unlink($articles->file_path);
        	$this->tableprice_model->delete_articles($articles_id);
            die('yes');
        }
    }

}

